package com.xiang.plane.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.xiang.plane.model.Background;
import com.xiang.plane.model.Boss;
import com.xiang.plane.model.Bullet;
import com.xiang.plane.model.Enemy;
import com.xiang.plane.model.Player;
import com.xiang.plane.util.Timer;

import java.util.Vector;

/**
 * Created by XY on 2017/8/9.
 * 游戏视图，调用逻辑
 *
 */

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder surfaceHolder;
    //画笔
    private Paint paint;
    //画布
    private Canvas canvas;
    //屏幕的宽和高
    public static int screenW, screenH;
    //游戏的背景
    private Background background;
    //玩家
    public static Player player;
    //分数
    public static int Score;

    //子弹集合
    public static Vector<Bullet> bulletVector = new Vector<>();
    //敌机集合
    public static Vector<Enemy> enemyVector = new Vector<>();
    //boss集合
    public static Vector<Boss> bossVector = new Vector<>();
    //产生敌机的计时器
    private Timer timer = new Timer(100);

    //控制游戏逻辑和绘制界面的线程
    private volatile boolean running = true;
    private final int fps = 40;

    public GameView(Context context) {
        super(context);
        surfaceHolder = this.getHolder();
        surfaceHolder.addCallback(this);
        paint = new Paint(Color.RED);
        paint.setAntiAlias(true);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setKeepScreenOn(true);
    }

    /**
     * 创建时初始化
     * @param surfaceHolder
     */
    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        screenW = getWidth();
        screenH = getHeight();
        initGame();
        running = true;
        //实例化线程
        Thread thread = new Thread(this);
        //启动线程
        thread.start();
    }

    //视图创建时，初始化
    private void initGame() {
        //实例化对象
        background = new Background();
        player = new Player(getResources());
        Boss boss = new Boss(getResources());
        bossVector.add(boss);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        running = false;
    }

    /**
     * 触屏事件，移动飞机
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //判断点击范围,在玩家飞机周围才移动飞机
        /*++++++++还需要加入移动速度的判断+++++++++++++++++++++++*/
        int x = (int) event.getX();
        int y = (int) event.getY();
        if(x > player.getPlayerX() -100 && x < player.getPlayerX() + player.getPlaneWidthHalf() + 100
                && y > player.getPlayerY() - 100 && y < player.getPlayerY() + player.getPlaneHeight() +100 ){
            player.setPlayerX(x);
            player.setPlayerY(y);
        }
        return true;
    }

    //在子线程中处理游戏
    @Override
    public void run() {
        while (running) {
            long start = System.currentTimeMillis();
            myDraw();
            logic();
            long end = System.currentTimeMillis();
            try {
                //时间均衡处理
                int sleepTime = 1000 / fps;
                if (end - start < sleepTime) {
                    Thread.sleep(sleepTime - (end - start));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //绘画方法
    public void myDraw() {
        try {
            canvas = surfaceHolder.lockCanvas();
            if (canvas != null) {
                canvas.drawColor(Color.WHITE);
                //根据状态来绘制
                background.draw(canvas, paint);
                player.draw(canvas, paint);
                //绘制敌机
                for (int i = 0; i < enemyVector.size(); i++) {
                    enemyVector.get(i).draw(canvas, paint);
                }
                //绘制子弹
                for (int i = 0; i < bulletVector.size(); i++) {
                    bulletVector.get(i).drawBullet(canvas, paint);
                }
                //绘制boss
                if (Score >= 20) {//第一只
                    bossVector.get(0).draw(canvas, paint);
                }
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    //游戏逻辑
    private void logic() {
        //背景逻辑
        background.logic();
        //玩家
        player.logic();
        //添加敌机
        if (timer.act()) {
            new Enemy(getResources());
        }
        //敌机
        for (int i = 0; i < enemyVector.size(); i++) {
            enemyVector.get(i).logic();
        }
        //子弹
        for (int i = 0; i < bulletVector.size(); i++) {
            bulletVector.get(i).logic();
        }
        //Boss
        if (Score >= 20) {//第一只
            bossVector.get(0).logic();
        }
    }

}
